package cn.cloud.butler.webapi.service;

import cn.cloud.butler.webapi.domain.RoleTEntity;
import cn.cloud.butler.webapi.repository.RoleTRepository;
import cn.cloud.butler.webapi.service.cache.RoleTCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouguocheng on 2018-12-21-上午 9:16
 */
@Service
public class RoleTService {
    private final RoleTRepository roleTRepository;

    private  final RoleTCache roleTCache;
    @Autowired
    public RoleTService(RoleTRepository roleTRepository, RoleTCache roleTCache) {
        this.roleTRepository = roleTRepository;
        this.roleTCache = roleTCache;
    }

    @Cacheable(value = "GetRole-key")
    public  List<Map<String,Object>> GetRole() {
       List<Map<String,Object>> listMap= new ArrayList();
        List<RoleTEntity>  roleTEntities =  roleTRepository.GetRole();
        for (RoleTEntity roleTEntity : roleTEntities) {
            HashMap roleMap=new HashMap();
            roleMap.put("role",roleTEntity.getRole());
            roleMap.put("rolename",roleTEntity.getRolename());
            roleMap.put("compid",roleTEntity.getCompType());
            listMap.add(roleMap);
        }
        return  listMap;
    }


    @Cacheable(value = "GetRoleByCompid-key")
    public List<Map<String,Object>> GetRoleByCompid(String compid, String pk) {
        List<Map<String,Object>> listResut = null;
        Integer  compId= Integer.parseInt(compid);
        List<RoleTEntity> RoleTEntites = null;
       if(pk.equals("all")){
           RoleTEntites = roleTCache.getRoleTByCompType(compId);
       }else  if(pk.equals("app")){
           RoleTEntites = roleTCache.getRoleTByRoleType(compId);
       }else{
           RoleTEntites = roleTCache.getRoleTByWeb(compId);
       }
        for (RoleTEntity roleTEntite : RoleTEntites) {
            Map<String,Object> map = new HashMap<>();
            map.put("rolename",roleTEntite.getRolename());
            map.put("roleid",roleTEntite.getRole());
            listResut.add(map);
       }
        return  listResut;

    }
}
